package indi.juzhuohan.chatroom.server;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import indi.juzhuohan.chatroom.constants.Constants;

/**
 * 服务器参数界面和日志界面
 * 
 * @author 鞠卓瀚
 */
public class ServerUI {

	public JTextPane txtLog;
	public JTextField txtServerName;
	public JTextField txtIP;
	public JTextField txtNumber;

	private static final String Background_Path = "G:\\java\\chatroom_socket\\src\\indi\\juzhuohan\\chatroom\\client\\chat_background.jpg";

	/**
	 * 服务器管理UI
	 * 
	 * @return
	 */
	public JLabel getServerUI() {
		JPanel pnlServer = new JPanel();
		pnlServer.setOpaque(false);
		pnlServer.setLayout(null);
		pnlServer.setBounds(0, 0, Serverframe.FRAME_WIDTH, Serverframe.FRAME_HEIGHT);
//		添加内容：参数界面、日志界面、保存日志按钮
		pnlServer.add(getServerParam());
		pnlServer.add(getServerlog());
		pnlServer.add(saveLogBtn());
		ImageIcon imageIcon = new ImageIcon(Background_Path);
		JLabel lblBackground = new JLabel(imageIcon);
		lblBackground.setBounds(0, 200, 300, 300);
		lblBackground.add(pnlServer);

		return lblBackground;
	}

	/**
	 * 服务器日志UI
	 * 
	 * @return
	 */
	public JPanel getServerlog() {
		JPanel serverLogPanel = new JPanel();
		serverLogPanel.setOpaque(false);
		serverLogPanel.setLayout(null);
		serverLogPanel.setBounds(130, 5, 380, 380);

		JLabel lblLog = new JLabel("[服务器日志]");
		lblLog.setForeground(Color.BLACK);
		lblLog.setFont(new Font("黑体", 0, 16));
		lblLog.setBounds(10, 5, 100, 30);
		serverLogPanel.add(lblLog);

		txtLog = new JTextPane();
		txtLog.setOpaque(false);
		txtLog.setFont(new Font("黑体", 0, 12));
		JScrollPane scoPaneOne = new JScrollPane(txtLog);
		scoPaneOne.setBounds(10, 35, 340, 350);
		scoPaneOne.getViewport().setOpaque(false);
		serverLogPanel.add(scoPaneOne);
		return serverLogPanel;
	}

	/**
	 * 服务器参数UI
	 * 
	 * @return
	 */
	public JPanel getServerParam() {
		JPanel serverParamPanel = new JPanel();
		serverParamPanel.setOpaque(false);
		serverParamPanel.setBounds(6, 5, 106, 405);
		serverParamPanel.setFont(new Font("黑体", 0, 14));
		serverParamPanel.setBorder(BorderFactory.createRaisedBevelBorder());
//		当前在线人数
		JLabel lblNumber = new JLabel("当前在线人数：");
		lblNumber.setFont(new Font("黑体", 0, 14));
		serverParamPanel.add(lblNumber);
		txtNumber = new JTextField("0 人", 10);
		txtNumber.setFont(new Font("黑体", 0, 14));
		txtNumber.setEditable(false);
		serverParamPanel.add(txtNumber);
//		服务器名称
		JLabel lblServerName = new JLabel("服务器名称");
		lblServerName.setFont(new Font("黑体", 0, 14));
		serverParamPanel.add(lblServerName);
		txtServerName = new JTextField(10);
		txtServerName.setFont(new Font("黑体", 0, 14));
		txtServerName.setEditable(false);
		serverParamPanel.add(txtServerName);
//		服务器IP
		JLabel lblIP = new JLabel("服务器IP：");
		lblIP.setFont(new Font("黑体", 0, 14));
		serverParamPanel.add(lblIP);
		txtIP = new JTextField(10);
		txtIP.setFont(new Font("黑体", 0, 14));
		txtIP.setEditable(false);
		serverParamPanel.add(txtIP);
//		服务器端口
		JLabel lblPort = new JLabel("服务器端口：");
		lblPort.setFont(new Font("黑体", 0, 14));
		serverParamPanel.add(lblPort);
		JTextField txtPort = new JTextField(Constants.SERVER_PORT + "", 10);
		txtPort.setFont(new Font("黑体", 0, 14));
		txtPort.setEditable(false);
		serverParamPanel.add(txtPort);

		return serverParamPanel;
	}

	/**
	 * 保存日志按钮UI־
	 * 
	 * @return
	 */
	public JButton saveLogBtn() {
		JButton saveLogBtn = new JButton("保存日志");
		saveLogBtn.setBackground(Color.WHITE);
		saveLogBtn.setFont(new Font("黑体", 0, 14));
		saveLogBtn.setBounds(190, 392, 220, 30);
		saveLogBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String logString = txtLog.getText();

				try (BufferedWriter out = new BufferedWriter(new FileWriter(new Constants().log_txtUrl))) {
					out.write(logString);
					out.close();
				} catch (IOException e1) {
					System.out.println("ui");
				}
				JOptionPane.showConfirmDialog(null, "日志文件保存成功", "保存成功", -1);
			}
		});
		return saveLogBtn;
	}
}
